package com.ruoyi.smartai.config;

import com.ruoyi.smartai.service.impl.DialogueServiceImpl;
import com.ruoyi.smartai.utils.WhatsAPPUtils;
import com.ruoyi.system.domain.FansInfo;
import com.ruoyi.system.domain.StandardScript;
import com.ruoyi.system.mapper.FansInfoMapper;
import com.ruoyi.system.mapper.StandardScriptMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.redis.RedisCache;

import java.time.LocalTime;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component // 将 Job 注册为 Spring 的 Bean
public class MessageJob implements Job {
    private static final Logger log = LoggerFactory.getLogger(MessageJob.class);

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private FansInfoMapper fansInfoMapper;

    @Autowired
    private StandardScriptMapper standardScriptMapper;

    @Value("${apiSevicePath.secketSendmessage}")
    private String newChatURL;


    @Override
    public void execute(JobExecutionContext context) {
        try {
            // 获取触发器信息
            Trigger trigger = context.getTrigger();
            String triggerName = trigger.getKey().getName();
            if (triggerName.contains("onWorkJob_")) {
                // 获取任务参数
                String customerId = context.getJobDetail().getJobDataMap().getString("customerId");
                boolean isWork = context.getJobDetail().getJobDataMap().getBoolean("isOnWork");
                redisCache.setCacheObject("persona:customer:" + customerId + ":work", isWork ? 1 : 0);
                if (isWork) {
                    log.info("晚安");
                    FansInfo info = new FansInfo();
                    info.setCustomerNumber(customerId);
                    List<FansInfo> fansInfoList = fansInfoMapper.selectInLineCustomer(info);
                    CompletableFuture.runAsync(() -> fansInfoList.stream().forEach(fansInfo -> {
                        try {
                            String keys = "persona:fans:" + fansInfo.getClientNumber() + ":statics";
                            String fansStatic = redisCache.getCacheObject(keys);
                            if (fansStatic.equals("1")) {
                                return;
                            }
                            StandardScript standardScript = standardScriptMapper.selectStandardScriptById("1");
                            String replayContent = standardScript.getCustomerReply();
                            WhatsAPPUtils.sendMessage(newChatURL, fansInfo.getCustomerNumber(), fansInfo.getClientNumber(), "0",
                                    replayContent, "1", true);
                        } catch (Exception e) {
                            log.error("JobExecutionContext:" + e);
                        }
                    }));
                }
            }
        } catch (Exception e) {
            log.error("MessageJob error:" + e);
        }
    }

}
